﻿namespace BM.Tools.WebControls.MForm.Design.PropertiesGeneration
{            
    using System;
    using System.Collections.Generic;
    using System.Linq;    
    using System.Windows.Forms;

    using BM.Tools.WebControls.MForm.Controls;
    using EnvDTE;

    /// <summary>
    /// This class represents a form which is used for defining properties that could be generated from the single instance templates.
    /// </summary>    
    public partial class PropertiesForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertiesForm"/> class.
        /// </summary>
        public PropertiesForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertiesForm"/> class.
        /// </summary>
        /// <param name="root">
        /// The MForm root control.
        /// </param>
        /// <param name="projectItem">
        /// The associated project item.
        /// </param>
        public PropertiesForm(Root root, ProjectItem projectItem) : this()
        {
            Root = root;
            ProjectItem = projectItem;
            InitializeRead();                                                           
        }

        /// <summary>
        /// Gets the associated project item in which the generation will take place.
        /// </summary>
        public ProjectItem ProjectItem
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets property definitions from the grid view.
        /// </summary>
        public IEnumerable<PropertyDefinition> PropertyDefinitions
        {
            get
            {
                var source = propertiesGridView.DataSource as BindingSource;
                return source.List.Cast<PropertyDefinition>();
            }
        }

        /// <summary>
        /// Gets the MForm root control for which the properties will be generated.
        /// </summary>
        public Root Root
        {
            get; 
            private set;
        }

        /// <summary>
        /// Reads the single instance templates of the specified root control into the grid view.
        /// </summary>
        private void InitializeRead()
        {
            var propertiesObtainer = new RootPropertiesObtainer(Root);
            var bindingSource = new BindingSource();
            foreach (var property in propertiesObtainer.Properties)
            {
                bindingSource.Add(property);
            }

            propertiesGridView.DataSource = bindingSource;
        }

        private void GenerateBtnClick(object sender, EventArgs e)
        {
            try
            {
                var enabledProperties = PropertyDefinitions.Where(property => property.Enabled);
                var generator = new PropertiesGenerator(Root, ProjectItem, enabledProperties);
                generator.Generate();
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show("A code generation error occurred: " + ex.Message);
            }
        }

        private void InverseSelectionBtnClick(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in propertiesGridView.Rows)
            {
                row.Cells["EnabledColumn"].Value = !(bool)row.Cells["EnabledColumn"].Value;
            }
        }
    }
}
